<template>
	<view class="test-page">
		<web-view v-if="showWebview" id="webview_id" :src="src" @message="handleMessage"></web-view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	// const SRC = 'http://172.16.23.101:5501/routePlan/index.html'
	const SRC = 'https://zhatu.deqing.gov.cn/polygonDraw/routePlan/index.html'
	export default {
		onLoad(options) {
			console.log(options)
			this.isClosed = options.isClosed
			this.src = `${SRC}?start=${options.start}&end=${options.end}&type=${options.type}`
			console.log(this.src)
			this.webViewContext = my.createWebViewContext('webview_id');
			// 向webview发送信息
			// this.webViewContext.postMessage();
		},
		destroyed() {
			clearInterval(this.timer)
			this.timer = null
		},
		mounted() {
			this.showWebview = true
			
			if (this.isClosed != '0') {
				// setTimeout(() => {
				// 	this.webViewContext.postMessage({
				// 		routeText: this.infoApprove.transitRouteText,
				// 		routePath: this.infoApprove.transitRoute,
				// 	});
				// }, 1000)
				this.timer = setInterval(() => {
					// 向webview发送信息
					this.webViewContext.postMessage({
						routeText: this.infoApprove.transitRouteText,
						routePath: this.infoApprove.transitRoute,
					});
				}, 200)
			}
		},
		computed: {
			...mapState('mdApprove', ['infoApprove']),
		},
		data() {
			return {
				src: '',
				webViewContext: null,
				showWebview: false,
				isClosed: '',
				timer: null,
			}
		},
		methods: {
			// webview向app发送信息
			handleMessage({
				detail
			}) {
				console.log('收到webview信息', detail)
				if (detail.type == 'path') {
					uni.$emit('busSetRoutePath', detail.data)
				}
				if (detail.closeMap) {
					uni.navigateBack()
				}
			},
		},
	}
</script>

<style lang="scss" scoped>
	.test-page {
		width: 100%;
		height: 100%;

		#webview_id {
			width: 100%;
			height: 100%;
		}
	}
</style>
